﻿using System;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using System.IO;
using System.Xml;
using System.Web;
//using System.Web.Script.Serialization;
//using System.Web.Script.Services;
using System.Web.Services;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Dunite.InfoRoad
{
    public partial class SaveForm : LayoutsPageBase
    {
        [WebMethod]
        public static string StoreForm(object lines)
        {
            try
            {
                string line = "Kalle";
                int index = line.IndexOf(",\"__ko_mapping__\"");
                string noKO = line.Substring(0, index) + "}";
                XmlDocument xml = Newtonsoft.Json.JsonConvert.DeserializeXmlNode(noKO);

                string filename = "Test.xml"; // Request.QueryString["Filename"];
                if (filename == "undefined")
                    filename = Path.GetRandomFileName() + ".xml";

                string filepath = "C:\\Users\\magnus.DUNITE\\Documents\\visual studio 2012\\Projects\\WebApplication3\\WebApplication3\\FormStorage\\" + filename;// Server.MapPath("~/FormStorage/" + filename);
                if (File.Exists(filepath))
                {
                    File.Delete(filepath);
                }
                using (FileStream fs = File.OpenWrite(filepath))
                {
                    xml.Save(fs);
                }


            }
            catch (Exception error)
            {
                string dostuff = error.Message;
            }
            return "OK";
        }
        [WebMethod]
        public static string SaveFormState(object lines)
        {
            try
            {
                string line = "Kalle";
                int index = line.IndexOf(",\"__ko_mapping__\"");
                string noKO = line.Substring(0, index) + "}";
                XmlDocument xml = Newtonsoft.Json.JsonConvert.DeserializeXmlNode(noKO);


                string filename = "Test.xml"; // Request.QueryString["Filename"];
                if (filename == "undefined")
                    filename = Path.GetRandomFileName() + ".xml";

                string filepath = "C:\\Users\\magnus.DUNITE\\Documents\\visual studio 2012\\Projects\\WebApplication3\\WebApplication3\\FormStorage\\" + filename;// Server.MapPath("~/FormStorage/" + filename);
                if (File.Exists(filepath))
                {
                    File.Delete(filepath);
                }
                using (FileStream fs = File.OpenWrite(filepath))
                {
                    xml.Save(fs);
                }


            }
            catch (Exception error)
            {
                string dostuff = error.Message;
            }
            return "OK";
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                if (Request.ContentType.Contains("json") && Request.QueryString["Filename"] != null)
                {
                    System.IO.StreamReader sr = new System.IO.StreamReader(Request.InputStream);
                    string line = "";
                    line = sr.ReadToEnd();
                    int index = line.IndexOf(",\"__ko_mapping__\"");
                    string noKO = line.Substring(0, index) + "}";
                    XmlDocument xml = Newtonsoft.Json.JsonConvert.DeserializeXmlNode(noKO);

                    string filename = Request.QueryString["Filename"];
                    if (filename == "undefined")
                        filename = Path.GetRandomFileName() + ".xml";

                    string filepath = Server.MapPath("~/FormStorage/" + filename);
                    if (File.Exists(filepath))
                    {
                        File.Delete(filepath);
                    }
                    using (FileStream fs = File.OpenWrite(filepath))
                    {
                        xml.Save(fs);
                    }

                }
            }
            catch (Exception error)
            {
                string dostuff = error.Message;
            }
            Response.Clear();
            Response.Write("");

        }

    }
}
